package com.zhanggm.springcloud.jpa.service.impl;

import com.zhanggm.springcloud.jpa.entity.TbArea;
import com.zhanggm.springcloud.jpa.entity.TbDepart;
import com.zhanggm.springcloud.jpa.entity.TbDocSkill;
import com.zhanggm.springcloud.jpa.repository.TbDoctorRepository;
import com.zhanggm.springcloud.jpa.service.*;
import com.zhanggm.springcloud.jpa.entity.TbDoctor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
@author Zhanggm
*/
@Service
public class TbDoctorServiceImpl implements TbDoctorService {

	@Resource
	private TbDoctorRepository doctorRepository;

	@Autowired
    private TbDocSkillService docSkillService;

	@Autowired
    private TbAreaService areaService;

	@Autowired
    private TbDepartService departService;

	@Autowired
    private TbUserService userService;

	@Override
	public Page<TbDoctor> getPageInfo(TbDoctor doctor, Integer pageNum, Integer pageSize) {
		Specification<TbDoctor> specification = new Specification<TbDoctor>() {
			@Override
			public Predicate toPredicate(Root<TbDoctor> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				//保存动态查询条件
                List<Predicate> predicateList = new ArrayList<>();
			    if(doctor.getName()!=null){
                    Predicate name = criteriaBuilder.like(root.get("name"), "%".concat(doctor.getName()).concat("%"));
                    predicateList.add(name);
                }
                if(doctor.getDepartId()!=null){
                    Predicate equal = criteriaBuilder.equal(root.get("departId"), doctor.getDepartId());
                    predicateList.add(equal);
                }

                if(doctor.getId()!=null){
                    Predicate id = criteriaBuilder.greaterThanOrEqualTo(root.get("id"), doctor.getId());
                    predicateList.add(id);
                }

                if(doctor.getId()!=null){
                    Predicate id = criteriaBuilder.lessThanOrEqualTo(root.get("id"), doctor.getId());
                    predicateList.add(id);
                }

                if(doctor.getDepartName()!=null){
                    Predicate like = criteriaBuilder.like(root.get("tbDepart").get("name"), "%".concat(doctor.getDepartName()).concat("%"));
                    predicateList.add(like);
                }
                if(doctor.getSkillName()!=null){
                    Predicate skills = criteriaBuilder.like(root.join("skills").get("name"), "%".concat(doctor.getSkillName()).concat("%"));
                    predicateList.add(skills);
                }
                /*Predicate[] predicateArr = new Predicate[predicateList.size()];
                predicateList.toArray(predicateArr);
                return criteriaBuilder.and(predicateArr);*/
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
		Pageable pageable = PageRequest.of(pageNum-1,pageSize, Sort.Direction.DESC,"id");
        Page<TbDoctor> all = doctorRepository.findAll(specification, pageable);
        all.getContent().forEach(doctor1 -> {
            doctor1.setUserName(userService.getUserById(doctor1.getUserId()).getName());
        });
        return all;
	}

    @Override
    public int save(TbDoctor doctor) {
	    //保存医生信息
        TbDoctor tbDoctor = doctorRepository.save(doctor);
        //删除关系表数据
        docSkillService.delByDockerId(doctor.getId());
        //保存关系表
        List<Integer> skillIds = doctor.getSkillIds();
        if(skillIds!=null && skillIds.size()>0){
            List<TbDocSkill> list = new ArrayList<>();
            skillIds.forEach(skillId->{
                TbDocSkill docSkill = new TbDocSkill();
                docSkill.setDocId(tbDoctor.getId());
                docSkill.setSkillId(skillId);
                list.add(docSkill);
            });
            docSkillService.save(list);
        }
        return 1;
    }

    @Override
    public int delByIds(List<Integer> ids) {
        List<TbDoctor> allById = doctorRepository.findAllById(ids);
        doctorRepository.deleteInBatch(allById);
        return allById.size();
    }

    @Override
    public int delById(Integer id) {
        doctorRepository.deleteById(id);
	    return 0;
    }

    @Override
    public TbDoctor getById(Integer id) {
        TbDoctor doctor = doctorRepository.findById(id).get();
        return doctor;
    }

    @Override
    public TbDoctor getDetailById(Integer id) {
        TbDoctor doctor = doctorRepository.findById(id).get();
        TbArea sheng = areaService.getById(doctor.getShengId());
        doctor.setShengName(sheng.getName());
        TbArea shi = areaService.getById(doctor.getShiId());
        doctor.setShiName(shi.getName());
        TbArea xian = areaService.getById(doctor.getXianId());
        doctor.setXianName(xian.getName());
        TbDepart depart = departService.getById(doctor.getDepartId());
        doctor.setDepartName(depart.getName());
        return doctor;
    }
}
